package com.yourwork.service.imp;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.yourwork.dao.LoginDao;
import com.yourwork.service.LoginService;
@Service(value="loginService")
public class LoginSeviceImp implements LoginService {
@Autowired
@Qualifier(value="loginDao")
private LoginDao loginDao;
	//判断是否登录成功 并返回
	public String loginSelect(String number,String password) {
		// TODO Auto-generated method stub
		//返回的结果
		String result;
		if(StringUtils.hasText(number)){
			if(StringUtils.hasText(password)){
		if(loginDao.login(number, password)!= null){
			result = "登录成功";
			System.out.println(result);
		}
	   else{
		   result="密码错误或账号不存在";
		}
		}else{
			result="密码不能为空";
		}
		}else{
			result="账号不能为空";
		}
		return result;
	}
	//判断登录返回的地址
	public String loginJudge(String result) {
		// TODO Auto-generated method stub
		if(result.equals("登录成功")){
			System.out.println("进入员工管理界面");
			return "redirect:/clientele/select";
		}else{
		return "login/login";
				}
	}
	@Transactional
	public String changePassword(String number, String password,
			String newPassword_1, String newPassword_2) {
		// TODO Auto-generated method stub
		//返回的结果
		String result;
		if(StringUtils.hasText(password)){
			if(newPassword_1 !=null && newPassword_2 !=null){
				if(newPassword_1.equals(newPassword_2)){
			if(loginDao.login(number, password)!= null){
				loginDao.changePassword(number,newPassword_1);
				result ="密码修改成功";
			}else{
				result="密码输入错误";
			}
			}else{
				result="两次密码输入不一致";
			}
			}else{
			   result="新密码不能为空";
			}
			}else{
				result="旧密码不能为空";
			}
		return result;
	}
	public String cancellation(HttpSession session) {
		// TODO Auto-generated method stub
		session.invalidate();  
		return "退出登录成功";
	}
	public String forgotPasswordResult(String result) {
		// TODO Auto-generated method stub
		if(result=="密码修改成功"){
			return "redirect:/login/select";
		}else{
			return "redirect:/clientele/select";
		}
		
	}
		

}
